package controller

import (
	. "api/model"
	. "api/util"
	"api/service"
	"net/http"
	"strconv"
	"github.com/gin-gonic/gin"
)

func GetWithdraw(c *gin.Context) {
	status := c.Query("status")
	phone := c.Query("phone")

	w := Withdraw{}

	if status != "" || phone != "" {
		ret, err := w.QueryWithdraw(status, phone)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"error": err.Error()})
			return
		}

		if ret == nil {
			ret = make([] *Withdraw, 0)
		}

		c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
	} else {
		ret, err := w.GetWithdraw()
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"error": err.Error()})
			return
		}

		if ret == nil {
			ret = make([] *Withdraw, 0)
		}

		c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
	}
}

func CreateWithdraw(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	claims, err := ParseCliamsToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	cid := int(claims.Id)

	w := Withdraw{}
	err = c.BindJSON(&w)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	err = w.CreateWithdraw(w, cid)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success"})
}

func UpdateWithdraw(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	w := Withdraw{}

	err = c.BindJSON(&w)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	status := w.Status
	if status != 0 && status != 1 && status != 2 && status != -1 {
		c.JSON(http.StatusOK, gin.H{"error": "status值错误"})
		return
	}

	err = w.UpdateWithdraw(id, status)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success"})
}

func UpdateWithdrawState(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	w := Withdraw{}
	err = c.BindJSON(&w)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	batchId := w.BatchId
	if batchId == "" {
		c.JSON(http.StatusOK, gin.H{"error": "batch_id值错误"})
		return
	}

	err = w.UpdateWithdrawState(id, batchId)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success"})
}

func GetIncomeRecord(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	w := Withdraw{}
	ret, err := w.GetIncomeRecord(id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	if ret == nil {
		ret = make([]*IncomeDetails, 0)
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
}

func GetWithdrawRecord(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	w := Withdraw{}
	ret, err := w.GetWithdrawRecord(id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	if ret == nil {
		ret = make([]*WithdrawRecord, 0)
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
}

func GetMakerAuthInfo(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	claims, err := ParseCliamsToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	cid := int(claims.Id)
	phone := c.Query("phone")
	cm := Customer{}
	err = cm.AuthUser(cid, phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	ret, err := service.GetMakerAuthInfoService(phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, ret)
}

func GetMakerAuthUrl(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	claims, err := ParseCliamsToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	cid := int(claims.Id)
	phone := c.Query("phone")
	returnUrl := c.Query("return_url")

	cm := Customer{}
	err = cm.AuthUser(cid, phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	ret, err := service.GetMakerAuthUrlService(phone, returnUrl)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, ret)
}

func GetMakerSignUrl(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	claims, err := ParseCliamsToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	cid := int(claims.Id)
	phone := c.Query("phone")
	returnUrl := c.Query("return_url")

	cm := Customer{}
	err = cm.AuthUser(cid, phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	ret, err := service.GetMakerSignUrlService(phone, returnUrl)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, ret)
}

